﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.SANY.Entity;
using PMS.SANY.IBusiness;
using PMS.SANY.Util;
using PMS.SANY.Models;

namespace PMS.SANY.Business
{
    public class PlanBusiness : BusRepository<Plan>, IPlanBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public PlanBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        private PlanStatus ToPlanStatus(int iStatus)
        {
            PlanStatus result = PlanStatus.Accept;
            switch (iStatus)
            {
                case 0: result = PlanStatus.Accept;
                    break;
                case 1:
                    result = PlanStatus.Execute;
                    break;
                case 2:
                    result = PlanStatus.Complete;
                    break;
                case 3:
                    result = PlanStatus.Cancel;
                    break;
                case 4:
                    result = PlanStatus.Pending;
                    break;
            }

            return result;
        }

        public async Task<PageResult<Plan>> GetPageResultAsync(PageInput<PlanQM> query)
        {
            var queryable = this.GetQueryable(true);
            var search = query.Search;
            if(search.Status>-1)
            {
                queryable = queryable.Where(p => p.Status == ToPlanStatus(search.Status));
            }

            if (!search.WipOrderNo.IsNullOrEmpty())
                queryable = queryable.Where(w => w.WipOrderNo.Contains(search.WipOrderNo));

            //var startDate = "";
            var endDate = "";

            var startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-01");
            //var endDate = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            if (!search.ScheduledStartDate[0].IsNullOrEmpty())
            {
                startDate = search.ScheduledStartDate[0].Substring(0, 10);
            }

            queryable = queryable.Where(w => String.Compare(w.ScheduledStartDate, startDate) >= 0);
            if (!search.ScheduledStartDate[1].IsNullOrEmpty())
            {
                endDate = search.ScheduledStartDate[1].Substring(0, 10) + " 23:59:59";
                queryable = queryable.Where(w => String.Compare(w.ScheduledStartDate, endDate) <= 0);
            }
            //
            queryable = queryable.Where(w => String.Compare(w.ScheduledStartDate, startDate) >= 0 && String.Compare(w.ScheduledStartDate, endDate) <= 0);
            //queryable = queryable.Where(w => Convert.ToDateTime(w.ScheduledStartDate) >= startDate && Convert.ToDateTime(w.ScheduledStartDate) <= endDate);

            var result = await this.GetPageResultAsync(queryable, query);
            if (!search.Id.IsNullOrEmpty())
            {
                if (!result.Data.Any(w => w.Id == search.Id))
                {
                    var idOne = await this.GetQueryable(true).SingleOrDefaultAsync(w => w.Id == search.Id);
                    result.Data.Add(idOne);
                    result.Total += 1;
                }
            }
            return result;
        }
    }
}
